package com.uriuniv.dao;

import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import com.uriuniv.domain.Student;
import com.uriuniv.domain.University;
import com.uriuniv.mapper.StudentM;
import com.uriuniv.mapper.UniversityM;

public class StudentD {

	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public Student getStudentByName(String name) throws SQLException {
		final String STUDENT = "SELECT * FROM STUDENT WHERE NAME = '" + name + "'";
		Student student = (Student)jdbcTemplate.queryForObject(STUDENT, new StudentM());
		return student;
	}
	
	public Student getStudentByEmail(String email) throws SQLException {
		final String STUDENT = "SELECT * FROM STUDENT WHERE EMAIL = '" + email + "'";
		Student student = (Student)jdbcTemplate.queryForObject(STUDENT, new StudentM());
		return student;
	}
	
	public int certification(String key) throws SQLException {
		
		final String CERTIFICATION = "UPDATE STUDENT SET CERTIFICATION = '1' WHERE CERTIFICATION = ?";
		
		int result = jdbcTemplate.update(CERTIFICATION,	new Object[] {key});
		
		return result;
	} 
	
	public int insertStudent(Student student) throws SQLException {
		
		final String INSERT_STUDENT = "INSERT INTO STUDENT VALUES (?,?,?,?,?,?,?,?,?,?,?)";
			
		int result = jdbcTemplate.update(INSERT_STUDENT,
					new Object[] {
						student.getStudentId(),
						student.getEmail(),
						student.getName(),
						student.getPassword(),
						student.getTempPassword(),
						student.getPoint(),
						student.getJoinDate(),
						student.getModDate(), 
						student.getGroupId().trim(), 
						student.getUnivId(), 
						student.getCertification()
					}
				);
		
		return result;
	}
	
}
